package cn.iocoder.yudao.module.album.service.imgsubject;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.album.controller.admin.imgsubject.vo.*;
import cn.iocoder.yudao.module.album.dal.dataobject.imgsubject.ImgSubjectDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.album.dal.mysql.imgsubject.ImgSubjectMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.album.enums.ErrorCodeConstants.*;

/**
 * 用户相册 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ImgSubjectServiceImpl implements ImgSubjectService {

    @Resource
    private ImgSubjectMapper imgSubjectMapper;

    @Override
    public Long createImgSubject(ImgSubjectSaveReqVO createReqVO) {
        // 插入
        ImgSubjectDO imgSubject = BeanUtils.toBean(createReqVO, ImgSubjectDO.class);
        imgSubjectMapper.insert(imgSubject);

        // 返回
        return imgSubject.getId();
    }

    @Override
    public void updateImgSubject(ImgSubjectSaveReqVO updateReqVO) {
        // 校验存在
        validateImgSubjectExists(updateReqVO.getId());
        // 更新
        ImgSubjectDO updateObj = BeanUtils.toBean(updateReqVO, ImgSubjectDO.class);
        imgSubjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteImgSubject(Long id) {
        // 校验存在
        validateImgSubjectExists(id);
        // 删除
        imgSubjectMapper.deleteById(id);
    }

    @Override
        public void deleteImgSubjectListByIds(List<Long> ids) {
        // 删除
        imgSubjectMapper.deleteByIds(ids);
        }


    private void validateImgSubjectExists(Long id) {
        if (imgSubjectMapper.selectById(id) == null) {
            throw exception(IMG_SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public ImgSubjectDO getImgSubject(Long id) {
        return imgSubjectMapper.selectById(id);
    }

    @Override
    public PageResult<ImgSubjectDO> getImgSubjectPage(ImgSubjectPageReqVO pageReqVO) {
        return imgSubjectMapper.selectPage(pageReqVO);
    }

}